import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class QwsScanGunContainer extends StatefulWidget {
  Container child;

  QwsScanGunContainer({super.key, required this.child});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _QwsScanGunState();
  }
}

class _QwsScanGunState extends State<QwsScanGunContainer> {
  int _lastKeyboardDown = 0;
  String _scanText = "";
  late FocusNode _focusNode;

  @override
  void initState() {
    _focusNode = FocusNode(
      onKeyEvent: (node, event) {
        print("onKeyEvent : ${event.character}");
        if(event.character == 'a') {
          return KeyEventResult.ignored;
        }
        return KeyEventResult.handled;
      }
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return KeyboardListener(
      focusNode: FocusNode(),
      autofocus: true,
      child: Container(
        width: 360,
        height: 120,
        alignment: Alignment.centerLeft,
        decoration: const BoxDecoration(
          border: Border(
            top: BorderSide(color: Color(0xFFDFDFDF)),
            left: BorderSide(color: Color(0xFFDFDFDF)),
            right: BorderSide(color: Color(0xFFDFDFDF)),
            bottom: BorderSide(color: Color(0xFFDFDFDF)),
          ),
        ),
        child: TextField(
          focusNode: _focusNode,
          keyboardType: TextInputType.number,
        ),
      ),
      onKeyEvent: (event) {
        if (event is KeyDownEvent) {
          int gap = DateTime.now().millisecondsSinceEpoch - _lastKeyboardDown;
          print("gapTime : $gap");
          _lastKeyboardDown = DateTime.now().millisecondsSinceEpoch;
          print(event.logicalKey.keyLabel);
          _scanText += event.character ?? "";
          setState(() {});
        } else if (event is KeyUpEvent) {
          int upTime =
              DateTime.now().millisecondsSinceEpoch - _lastKeyboardDown;
          print("upTime : $upTime");
        }
      },
    );
  }
}
